def p(n):
    for i in range(2,n):
        if n%i==0:
            return False
    return True

x = int(input())
pr=[i for i in range(1, 10000) if p(i)]

m=0
if 1000<=x<=9999:
    for i in pr:
        if str(i) in str(x):
            m=max(i,m)
print(m)
